package com.cuitrip.business.order;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuitrip.apiservice.ServiceBusiness;
import com.cuitrip.app.UserConfig;
import com.cuitrip.business.order.model.PayInfo;
import com.cuitrip.business.tripservice.proxy.PriceProxy;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.component.para.ParaListView;
import com.cuitrip.component.para.model.Para;
import com.cuitrip.component.para.proxy.ParaProxy;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * Created on 7/18.
 */
public class PayDetailFragment extends CustomUiFragment {


    @Bind(R.id.order_pay_way_layout)
    ItemLayout mPaymentWayLayout;

    @Bind(R.id.order_pay_price_layout)
    ItemLayout mPayCountLayout;

    @Bind(R.id.pay_include_list)
    ParaListView mPayIncludeListView;

    @Bind(R.id.pay_exclude_list)
    ParaListView mPayExcludeListView;

    public static final String DATA_KEY = "data_key";

    private PayInfo data;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null && bundle.getSerializable(DATA_KEY) != null) {
            data = (PayInfo) bundle.getSerializable(DATA_KEY);
        } else {
            getActivity().finish();
        }
    }

   /* @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.page_pay_content, null);
        }
        ButterKnife.bind(this, mContentView);
        return mContentView;
    }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState,R.layout.page_pay_content);
    }

   /* @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showActionBar(getString(R.string.ct_price_desc));
        initView();
        initListener();
    }*/



    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
        if (ServiceInfo.PAYWAY_FREE.equals(data.getPaymentWay())) {
            mPayCountLayout.setVisibility(View.GONE);
        } else {
            mPayCountLayout.setVisibility(View.VISIBLE);
            mPayCountLayout.setmRightText(PriceProxy.getInstance().priceFormatTextStyled(data.getPaymentWay(),data.getPayCurrency(),data.getPrice()));
           // mPayCountLayout.setmRightText(data.getPayPriceWithCurrency());
        }
        mPaymentWayLayout.setmRightText(PayInfo.getPayWayText(data.getPaymentWay()));
        bindParaItem(mPayIncludeListView, data.getPayInclude());
        bindParaItem(mPayExcludeListView, data.getPayExclude());
    }

    /*private void initView() {
        if (ServiceInfo.PAYWAY_FREE.equals(data.getPaymentWay())) {
            mPayCountLayout.setVisibility(View.GONE);
        } else {
            mPayCountLayout.setVisibility(View.VISIBLE);
            mPayCountLayout.setmRightText(PriceProxy.getInstance().priceFormatTextStyled(data.getPaymentWay(), data.getPayCurrency(), data.getPrice()));
        }
        mPaymentWayLayout.setmRightText(PayInfo.getPayWayText(data.getPaymentWay()));
        bindParaItem(mPayIncludeListView, data.getPayInclude());
        bindParaItem(mPayExcludeListView, data.getPayExclude());
    }*/


    private void bindParaItem(ParaListView paraListView, String paraOutput) {
        if (!TextUtils.isEmpty(paraOutput)) {
            List<Para> paraList = new ArrayList<>();
            ArrayList<CharSequence> charSequenceList = ParaProxy.getInstance().getParaList(paraOutput);
            for (CharSequence charSequence : charSequenceList) {
                if (TextUtils.isEmpty(charSequence.toString().trim())){
                    continue;
                }
                paraList.add(new Para(charSequence));
            }
            paraListView.setParas(paraList);
        }else{
                List<Para> paraList = new ArrayList<>();
                paraList.add(new Para(ResourceUtil.getString(R.string.value_no_set)));
                paraListView.setParas(paraList);
        }
    }

    @Override
    public void initListener() {

    }

   /* @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }*/

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.price_group_detail);
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        getHostActivity().onBackPressed();
    }
}
